﻿#include "userregister.h"
#include "ui_userregister.h"
#include "baidufaceapi.h"
#include "msgapi.h"
#include <QMessageBox>

UserRegister::UserRegister(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::UserRegister) {
    ui->setupUi(this);
    devCamera = NULL;
    currentUInfo = new UInfo;
    currentUInfo->userId = "";
    currentUInfo->username = "";
    currentUInfo->picPath = "";
    currentUInfo->rfidCardNo = "";
    currentUInfo->createTime = "";
//    currentPicPath = "";
//    currentTimestamp = "";
//    currentRfidNo = "";
    isUpdate = 0;
}

UserRegister::~UserRegister() {
    delete ui;
    delete currentUInfo;
    if(devCamera != NULL) {
        delete devCamera;
        devCamera = NULL;
    }
}

void UserRegister::startCamera() {
    if(devCamera == NULL) {
        devCamera = new DeviceCamera(ui->camera_show);
    }
}

void UserRegister::stopCamera() {
    if(devCamera != NULL) {
        delete devCamera;
        devCamera = NULL;
    }
}

void UserRegister::showPic(const QString &picPath) {
    if (picPath.isEmpty() || !QDir().exists(picPath)) {  // 空路径&&路径不存在
        return;
    }
    QFileInfo FileInfo(picPath);
    if (!FileInfo.isFile()) { // 如果不是文件
        return;
    }
    QImage img;
    img.load(picPath);
    QPixmap pixmap(picPath);
    pixmap.scaled(ui->picture_show->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
    ui->picture_show->setScaledContents(true);
    ui->picture_show->setPixmap(pixmap);
}

void UserRegister::setIsUpdate() {
    isUpdate = 1;
}


void UserRegister::updateSet(const UInfo &uInfo) {
    currentUInfo->userId = uInfo.userId;
    currentUInfo->username = uInfo.username;
    currentUInfo->picPath = uInfo.picPath;
    currentUInfo->rfidCardNo = uInfo.rfidCardNo;
    currentUInfo->createTime = uInfo.createTime;
    ui->usernameLineEdit->setText(uInfo.username);
    showPic(uInfo.picPath);
}

void UserRegister::on_submitButton_clicked() {
    currentUInfo->username = ui->usernameLineEdit->text();
    if(currentUInfo->username.isEmpty()) {
        QMessageBox::warning(this, "警告信息", "请输入用户名");
        return;
    }

    if(currentUInfo->userId.isEmpty() || currentUInfo->picPath.isEmpty()) {
        QMessageBox::warning(this, "警告信息", "请拍摄照片");
        return;
    }

    bool result = BaiduFaceAPI::faceRegister(currentUInfo->picPath, currentUInfo->userId, currentUInfo->username);
    if(!result) {
        QMessageBox::information(this, "注册信息", "百度云AI平台注册失败");
        return ;
    }

    // 创建时间不可改
    if(isUpdate != 1) {
        currentUInfo->createTime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz");
    }

    result = MsgApi::getMsgApi()->userRegister(*currentUInfo);
    if(!result) {
        qDebug() << "on_submitButton_clicked send msg error";
    }

    qDebug() << "currentUInfo : " << currentUInfo->userId << "," << currentUInfo->username << ","
             << currentUInfo->picPath << "," << currentUInfo->createTime;

}

void UserRegister::on_photographButton_clicked() {

    // 防止多次拍照，图片占用空间
    if (!currentUInfo->picPath.isEmpty() && QDir().exists(currentUInfo->picPath)) {  // 非空路径&&路径存在
        QFileInfo FileInfo(currentUInfo->picPath);
        if (FileInfo.isFile()) { // 如果是文件
            QFile::remove(currentUInfo->picPath);
            currentUInfo->picPath = "";
        }
    }

    QString currentTimeStamp = QString::number(time(NULL));

    // 更新时不能改ID
    if(isUpdate != 1) {
        currentUInfo->userId = currentTimeStamp;
    }

    currentUInfo->picPath = "./register/register_" + currentTimeStamp + ".bmp";

    // 拍照
    devCamera->capturePic(currentUInfo->picPath);

    // 显示图片
    showPic(currentUInfo->picPath);
}

void UserRegister::on_rfidButton_clicked() {
    QMessageBox::warning(this, "将RFID卡放入感应区", "请在10秒内完成");
}
